class TeacherController < ApplicationController

  def index
  end

  def new
    @teacher = Teacher.new
  end

  def create
    @teacher = Teacher.new(params[:teacher])
    @address = @teacher.address = Address.new(params[:address])
    
    if @teacher.save
      flash[:notice] = 'Teacher successfully added'
      APPLOG.info 'Teacher #' + @teacher.id.to_s + ' added into the system'
      redirect_to :action => 'index'
    else
      flash[:notice] = 'Teacher unsuccessfully added'
      render :action => 'new'
    end
  end

  def edit
    @teacher = Teacher.get_by_id(params[:id])
  end

  def update
    @teacher = Teacher.get_by_id(params[:id])
    @address = @teacher.address
    
    if @teacher.update_attributes(params[:teacher]) && @address.update_attributes(params[:address])
      flash[:notice] = 'Teacher successfully updated'
      APPLOG.info 'Teacher #' + @teacher.id.to_s + ' updated'
      redirect_to :action => 'list'
    else
      APPLOG.info 'Teacher unsuccessfully updated'
      render :action => 'edit'
    end
  end

  def show
    @teacher = Teacher.get_by_id(params[:id])
  end

  def list
    @teachers = Teacher.get_all
  end
end
